var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new HeadlineRemote(app);
};

var HeadlineRemote = function (app) {
    this.app = app;
};

// 新增跑马灯
HeadlineRemote.prototype.addHeadline= function (msg, callback) {
    logger.info("add headline msg=" + JSON.stringify(msg));
    if (!msg.hasOwnProperty("content")) {
        callback({resultCode: 404, message: "content为空"});
        return;
    }
    if (!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if (!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }
    if (!msg.hasOwnProperty("frequency")) {
        callback({resultCode: 404, message: "frequency为空"});
        return;
    }

    let headlineService = this.app.get("headlineService");
    let startTime = parseInt(msg.startTime);
    let endTime = parseInt(msg.endTime);
    let frequency = parseInt(msg.frequency);
    headlineService.addHeadline(msg.content, startTime, endTime, frequency, headline => {
        if(!!headline) {
            callback({
                resultCode: 0,
                message: "ok",
                headline : headline.toJSON(),
            });
        }else{
            callback({
                resultCode: 404,
                message: "add headline失败",
            });
        }
    });
};

// 更新跑马灯
HeadlineRemote.prototype.updateHeadline = function (msg, callback) {
    logger.info("update headline msg=" + JSON.stringify(msg));
    if (!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if (!msg.hasOwnProperty("content")) {
        callback({resultCode: 404, message: "content为空"});
        return;
    }
    if (!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if (!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }
    if (!msg.hasOwnProperty("frequency")) {
        callback({resultCode: 404, message: "frequency为空"});
        return;
    }

    let headlineService = this.app.get("headlineService");
    let id = parseInt(msg.id);
    let startTime = parseInt(msg.startTime);
    let endTime = parseInt(msg.endTime);
    let frequency = parseInt(msg.frequency);

    headlineService.updateHeadline(id, msg.content, startTime, endTime, frequency, headline => {
        if(!!headline) {
            callback({
                resultCode: 0,
                message: "ok",
                headline : headline.toJSON(),
            });
        }else{
            callback({
                resultCode: 404,
                message: "update headline失败",
            });
        }
    });
};

// 查询所有跑马灯
HeadlineRemote.prototype.findAllHeadlines = function (msg, callback) {
    logger.info("find all headlines msg=" + JSON.stringify(msg));

    let headlineService = this.app.get("headlineService");
    headlineService.findAllHeadlines(headlines => {
        callback({
            resultCode: 0,
            message: "ok",
            headline : headlineService.serializeHeadlines(headlines),
        });
    });
};

// 查询跑马灯
HeadlineRemote.prototype.findHeadlineById = function (msg, callback) {
    logger.info("find  headline by id msg=" + JSON.stringify(msg));
    if (!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }

    let headlineService = this.app.get("headlineService");
    let id = parseInt(msg.id);
    headlineService.findHeadlineById(id, headline => {
        callback({
            resultCode: 0,
            message: "ok",
            headline : headline != null ? headline.toJSON() : {}
        });
    });
};

// 根据id删除跑马灯
HeadlineRemote.prototype.deleteHeadline = function (msg, callback) {
    logger.info("delete headlines msg=" + JSON.stringify(msg));
    if (!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    let headlineService = this.app.get("headlineService");
    let id = parseInt(msg.id);
    headlineService.deleteHeadline(id, id => {
        if(!!id) {
            callback({
                resultCode: 0,
                message: "ok",
                headline : {id : id},
            });
        }else{
            callback({
                resultCode: 404,
                message: "delete headline失败",
            });
        }
    });
};






